<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>项目准备 | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../01-day/restful/django.html" />
    
    
    <link rel="prev" href="../../01-day/restful/method/response.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.1.4"
        data-chapter-title="项目准备"
        data-filepath="01-day/restful/prepare.md"
        data-basepath="../.."
        data-revision="Sun May 10 2020 14:49:33 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Django REST framework
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01-day.html">
            
                
                    <a href="../../01-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django REST framework流程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01-day/restful.html">
            
                
                    <a href="../../01-day/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        RESTfull
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="01-day/restful/mode.html">
            
                
                    <a href="../../01-day/restful/mode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="01-day/restful/restful.html">
            
                
                    <a href="../../01-day/restful/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="01-day/restful/method.html">
            
                
                    <a href="../../01-day/restful/method.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.</b>
                        
                        RESTful设计思想
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="01-day/restful/method/request.html">
            
                
                    <a href="../../01-day/restful/method/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.1.</b>
                        
                        请求相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="01-day/restful/method/response.html">
            
                
                    <a href="../../01-day/restful/method/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.2.</b>
                        
                        响应相关
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.1.4" data-path="01-day/restful/prepare.html">
            
                
                    <a href="../../01-day/restful/prepare.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.4.</b>
                        
                        项目准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="01-day/restful/django.html">
            
                
                    <a href="../../01-day/restful/django.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.5.</b>
                        
                        使用Django开发REST 接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="01-day/restful/task.html">
            
                
                    <a href="../../01-day/restful/task.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.6.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-day/restful/djangorestful-framework.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Django REST framework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-day/restful/djangorestful-framework/config.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="01-day/restful/djangorestful-framework/like.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/like.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Django REST framework魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02-day.html">
            
                
                    <a href="../../02-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        序列化和反序列化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="02-day/modify.html">
            
                
                    <a href="../../02-day/modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Serializer字段和选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="02-day/serializer.html">
            
                
                    <a href="../../02-day/serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="02-day/serializer/base.html">
            
                
                    <a href="../../02-day/serializer/base.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.1.</b>
                        
                        基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="02-day/serializer/assoc.html">
            
                
                    <a href="../../02-day/serializer/assoc.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.2.</b>
                        
                        关联对象嵌套序列化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="02-day/un-serializer.html">
            
                
                    <a href="../../02-day/un-serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        反序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="02-day/un-serializer/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1.1" data-path="02-day/un-serializer/validate/type.html">
            
                
                    <a href="../../02-day/un-serializer/validate/type.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.1.</b>
                        
                        字段类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.2" data-path="02-day/un-serializer/validate/field.html">
            
                
                    <a href="../../02-day/un-serializer/validate/field.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.2.</b>
                        
                        字段选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.3" data-path="02-day/un-serializer/validate/validatefieldname.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validatefieldname.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.3.</b>
                        
                        validate\_fieldname
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.4" data-path="02-day/un-serializer/validate/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.4.</b>
                        
                        validate
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="02-day/un-serializer/save.html">
            
                
                    <a href="../../02-day/un-serializer/save.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.2.</b>
                        
                        保存
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="02-day/modelserializer.html">
            
                
                    <a href="../../02-day/modelserializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03-day/view.html">
            
                
                    <a href="../../03-day/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03-day/view/requestresponse.html">
            
                
                    <a href="../../03-day/view/requestresponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Request 与 Response
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="03-day/view/requestresponse/request.html">
            
                
                    <a href="../../03-day/view/requestresponse/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.1.</b>
                        
                        Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="03-day/view/requestresponse/response.html">
            
                
                    <a href="../../03-day/view/requestresponse/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.2.</b>
                        
                        Response
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03-day/view/view.html">
            
                
                    <a href="../../03-day/view/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="03-day/view/view/apiview.html">
            
                
                    <a href="../../03-day/view/view/apiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.1.</b>
                        
                        APIView\(一级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="03-day/view/view/genericapiview.html">
            
                
                    <a href="../../03-day/view/view/genericapiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.2.</b>
                        
                        GenericAPIView\(二级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="03-day/view/view/mixin.html">
            
                
                    <a href="../../03-day/view/view/mixin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.3.</b>
                        
                        Mixin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="03-day/view/view/subclass.html">
            
                
                    <a href="../../03-day/view/view/subclass.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.4.</b>
                        
                        子类视图\(三级\)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03-day/view/viewset.html">
            
                
                    <a href="../../03-day/view/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        视图集ViewSet
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="03-day/view/viewset/viewset.html">
            
                
                    <a href="../../03-day/view/viewset/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="03-day/view/viewset/genericviewset.html">
            
                
                    <a href="../../03-day/view/viewset/genericviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GenericViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="03-day/view/viewset/modelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/modelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        ModelViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="03-day/view/viewset/readonlymodelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/readonlymodelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        ReadOnlyModelViewSet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="03-day/view/router.html">
            
                
                    <a href="../../03-day/view/router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="03-day/other.html">
            
                
                    <a href="../../03-day/other.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        高级功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="03-day/other/authentication.html">
            
                
                    <a href="../../03-day/other/authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        认证Authentication
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="03-day/other/quan-xian-permissions.html">
            
                
                    <a href="../../03-day/other/quan-xian-permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        权限Permissions
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="03-day/other/throttling.html">
            
                
                    <a href="../../03-day/other/throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        限流Throttling
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="03-day/other/filtering.html">
            
                
                    <a href="../../03-day/other/filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        过滤Filtering
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="03-day/other/order.html">
            
                
                    <a href="../../03-day/other/order.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        排序OrderingFilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="03-day/other/pagination.html">
            
                
                    <a href="../../03-day/other/pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        分页Pagination
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="03-day/other/exceptions.html">
            
                
                    <a href="../../03-day/other/exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        异常处理 Exceptions
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4F7F;&#x7528;django&#x5F00;&#x53D1;rest-&#x63A5;&#x53E3;">&#x9879;&#x76EE;&#x51C6;&#x5907; </h1>
<p>&#x521B;&#x5EFA;&#x9879;&#x76EE;+&#x521B;&#x5EFA;&#x5E94;&#x7528;+&#x5B89;&#x88C5;&#x5E94;&#x7528;+&#x914D;&#x7F6E;&#x6A21;&#x677F;&#x8DEF;&#x5F84;+&#x672C;&#x5730;&#x5316;+mysql&#x6570;&#x636E;&#x5E93;+URLconf+&#x89C6;&#x56FE;</p>
<ul>
<li><p>&#x5B89;&#x88C5;&#x5E94;&#x7528;&#xFF0C;&#x5173;&#x95ED;csrf&#x4E2D;&#x95F4;&#x4EF6;</p>
</li>
<li><pre><code class="lang-py">INSTALLED_APPS = [
    <span class="hljs-string">&apos;django.contrib.admin&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.auth&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.contenttypes&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.sessions&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.messages&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.staticfiles&apos;</span>,
    <span class="hljs-string">&apos;book.apps.BookConfig&apos;</span>
]

MIDDLEWARE = [
    <span class="hljs-string">&apos;django.middleware.security.SecurityMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.sessions.middleware.SessionMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.common.CommonMiddleware&apos;</span>,
    <span class="hljs-comment">#&apos;django.middleware.csrf.CsrfViewMiddleware&apos;,</span>
    <span class="hljs-string">&apos;django.contrib.auth.middleware.AuthenticationMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.messages.middleware.MessageMiddleware&apos;</span>,
    <span class="hljs-string">&apos;django.middleware.clickjacking.XFrameOptionsMiddleware&apos;</span>,
]
</code></pre>
</li>
<li><p>mysql&#x6570;&#x636E;&#x5E93;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x7684;book</p>
<pre><code>DATABASES = {
    &apos;default&apos;: {
        &apos;ENGINE&apos;: &apos;django.db.backends.mysql&apos;,
        &apos;HOST&apos;: &apos;127.0.0.1&apos;, # &#x6570;&#x636E;&#x5E93;&#x4E3B;&#x673A;
        &apos;PORT&apos;: 3306, # &#x6570;&#x636E;&#x5E93;&#x7AEF;&#x53E3;
        &apos;USER&apos;: &apos;root&apos;, # &#x6570;&#x636E;&#x5E93;&#x7528;&#x6237;&#x540D;
        &apos;PASSWORD&apos;: &apos;mysql&apos;, # &#x6570;&#x636E;&#x5E93;&#x7528;&#x6237;&#x5BC6;&#x7801;
        &apos;NAME&apos;: &apos;book&apos; # &#x6570;&#x636E;&#x5E93;&#x540D;&#x5B57;
    }
}
</code></pre></li>
<li><p>&#x6A21;&#x578B;&#xFF1A;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;</p>
</li>
<li><pre><code class="lang-py"><span class="hljs-comment"># &#x51C6;&#x5907;&#x4E66;&#x7C4D;&#x5217;&#x8868;&#x4FE1;&#x606F;&#x7684;&#x6A21;&#x578B;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfo</span><span class="hljs-params">(models.Model)</span>:</span>
    <span class="hljs-comment"># &#x521B;&#x5EFA;&#x5B57;&#x6BB5;&#xFF0C;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;...</span>
    name = models.CharField(max_length=<span class="hljs-number">20</span>, verbose_name=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>)
    pub_date = models.DateField(verbose_name=<span class="hljs-string">&apos;&#x53D1;&#x5E03;&#x65E5;&#x671F;&apos;</span>,null=<span class="hljs-keyword">True</span>)
    readcount = models.IntegerField(default=<span class="hljs-number">0</span>, verbose_name=<span class="hljs-string">&apos;&#x9605;&#x8BFB;&#x91CF;&apos;</span>)
    commentcount = models.IntegerField(default=<span class="hljs-number">0</span>, verbose_name=<span class="hljs-string">&apos;&#x8BC4;&#x8BBA;&#x91CF;&apos;</span>)
    is_delete = models.BooleanField(default=<span class="hljs-keyword">False</span>, verbose_name=<span class="hljs-string">&apos;&#x903B;&#x8F91;&#x5220;&#x9664;&apos;</span>)
    image = models.ImageField(upload_to=<span class="hljs-string">&apos;book&apos;</span>, null=<span class="hljs-keyword">True</span>, verbose_name=<span class="hljs-string">&apos;&#x56FE;&#x7247;&apos;</span>)

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        db_table = <span class="hljs-string">&apos;bookinfo&apos;</span>  <span class="hljs-comment"># &#x6307;&#x660E;&#x6570;&#x636E;&#x5E93;&#x8868;&#x540D;</span>
        verbose_name = <span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>  <span class="hljs-comment"># &#x5728;admin&#x7AD9;&#x70B9;&#x4E2D;&#x663E;&#x793A;&#x7684;&#x540D;&#x79F0;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5B9A;&#x4E49;&#x6BCF;&#x4E2A;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x7684;&#x663E;&#x793A;&#x4FE1;&#x606F;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">return</span> self.name
</code></pre>
<p>&#x8FC1;&#x79FB;&#xFF0C;&#x6267;&#x884C;&#x8FC1;&#x79FB;&#x540E;&#x63D2;&#x5165;&#x6570;&#x636E;</p>
<pre><code>insert into bookinfo(name, pub_date, readcount,commentcount, is_delete) values
(&apos;&#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&apos;, &apos;1980-5-1&apos;, 12, 34, 0),
(&apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;, &apos;1986-7-24&apos;, 36, 40, 0),
(&apos;&#x7B11;&#x50B2;&#x6C5F;&#x6E56;&apos;, &apos;1995-12-24&apos;, 20, 80, 0),
(&apos;&#x96EA;&#x5C71;&#x98DE;&#x72D0;&apos;, &apos;1987-11-11&apos;, 58, 24, 0);
</code></pre></li>
<li><p>&#x6A21;&#x578B;&#xFF1A;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;</p>
</li>
<li><pre><code class="lang-py"><span class="hljs-comment"># &#x51C6;&#x5907;&#x4EBA;&#x7269;&#x5217;&#x8868;&#x4FE1;&#x606F;&#x7684;&#x6A21;&#x578B;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PeopleInfo</span><span class="hljs-params">(models.Model)</span>:</span>
    GENDER_CHOICES = (
        (<span class="hljs-number">0</span>, <span class="hljs-string">&apos;male&apos;</span>),
        (<span class="hljs-number">1</span>, <span class="hljs-string">&apos;female&apos;</span>)
    )
    name = models.CharField(max_length=<span class="hljs-number">20</span>, verbose_name=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>)
    gender = models.SmallIntegerField(choices=GENDER_CHOICES, default=<span class="hljs-number">0</span>, verbose_name=<span class="hljs-string">&apos;&#x6027;&#x522B;&apos;</span>)
    description = models.CharField(max_length=<span class="hljs-number">200</span>, null=<span class="hljs-keyword">True</span>, verbose_name=<span class="hljs-string">&apos;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&apos;</span>)
    book = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>)  <span class="hljs-comment"># &#x5916;&#x952E;</span>
    is_delete = models.BooleanField(default=<span class="hljs-keyword">False</span>, verbose_name=<span class="hljs-string">&apos;&#x903B;&#x8F91;&#x5220;&#x9664;&apos;</span>)

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        db_table = <span class="hljs-string">&apos;peopleinfo&apos;</span>
        verbose_name = <span class="hljs-string">&apos;&#x4EBA;&#x7269;&#x4FE1;&#x606F;&apos;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__str__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.name
</code></pre>
<p>&#x8FC1;&#x79FB;&#xFF0C;&#x6267;&#x884C;&#x8FC1;&#x79FB;&#x540E;&#x63D2;&#x5165;&#x6570;&#x636E;</p>
<pre><code>insert into peopleinfo(name, gender, book_id, description, is_delete)  values
    (&apos;&#x90ED;&#x9756;&apos;, 1, 1, &apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;, 0),
    (&apos;&#x9EC4;&#x84C9;&apos;, 0, 1, &apos;&#x6253;&#x72D7;&#x68CD;&#x6CD5;&apos;, 0),
    (&apos;&#x9EC4;&#x836F;&#x5E08;&apos;, 1, 1, &apos;&#x5F39;&#x6307;&#x795E;&#x901A;&apos;, 0),
    (&apos;&#x6B27;&#x9633;&#x950B;&apos;, 1, 1, &apos;&#x86E4;&#x87C6;&#x529F;&apos;, 0),
    (&apos;&#x6885;&#x8D85;&#x98CE;&apos;, 0, 1, &apos;&#x4E5D;&#x9634;&#x767D;&#x9AA8;&#x722A;&apos;, 0),
    (&apos;&#x4E54;&#x5CF0;&apos;, 1, 2, &apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;, 0),
    (&apos;&#x6BB5;&#x8A89;&apos;, 1, 2, &apos;&#x516D;&#x8109;&#x795E;&#x5251;&apos;, 0),
    (&apos;&#x865A;&#x7AF9;&apos;, 1, 2, &apos;&#x5929;&#x5C71;&#x516D;&#x9633;&#x638C;&apos;, 0),
    (&apos;&#x738B;&#x8BED;&#x5AE3;&apos;, 0, 2, &apos;&#x795E;&#x4ED9;&#x59D0;&#x59D0;&apos;, 0),
    (&apos;&#x4EE4;&#x72D0;&#x51B2;&apos;, 1, 3, &apos;&#x72EC;&#x5B64;&#x4E5D;&#x5251;&apos;, 0),
    (&apos;&#x4EFB;&#x76C8;&#x76C8;&apos;, 0, 3, &apos;&#x5F39;&#x7434;&apos;, 0),
    (&apos;&#x5CB3;&#x4E0D;&#x7FA4;&apos;, 1, 3, &apos;&#x534E;&#x5C71;&#x5251;&#x6CD5;&apos;, 0),
    (&apos;&#x4E1C;&#x65B9;&#x4E0D;&#x8D25;&apos;, 0, 3, &apos;&#x8475;&#x82B1;&#x5B9D;&#x5178;&apos;, 0),
    (&apos;&#x80E1;&#x6590;&apos;, 1, 4, &apos;&#x80E1;&#x5BB6;&#x5200;&#x6CD5;&apos;, 0),
    (&apos;&#x82D7;&#x82E5;&#x5170;&apos;, 0, 4, &apos;&#x9EC4;&#x8863;&apos;, 0),
    (&apos;&#x7A0B;&#x7075;&#x7D20;&apos;, 0, 4, &apos;&#x533B;&#x672F;&apos;, 0),
    (&apos;&#x8881;&#x7D2B;&#x8863;&apos;, 0, 4, &apos;&#x516D;&#x5408;&#x62F3;&apos;, 0);
</code></pre></li>
<li><p>&#x89C6;&#x56FE;&#xFF1A;</p>
</li>
<li><pre><code class="lang-py"><span class="hljs-keyword">from</span> django.views.generic <span class="hljs-keyword">import</span> View
<span class="hljs-keyword">from</span> book.models <span class="hljs-keyword">import</span> BookInfo

<span class="hljs-comment"># Create your views here.</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookListView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x56FE;&#x4E66;&#x5217;&#x8868;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self,request)</span>:</span>

        <span class="hljs-keyword">return</span> JsonResponse({<span class="hljs-string">&apos;msg&apos;</span>:<span class="hljs-string">&apos;ok&apos;</span>})
</code></pre>
</li>
<li><p>URLconf</p>
<ul>
<li><p><code>settings.py</code>&#x4E2D;&#xFF1A;&#x6307;&#x5B9A;url&#x914D;&#x7F6E;</p>
<pre><code>ROOT_URLCONF = &apos;bookmanager.urls&apos;
</code></pre></li>
<li><p>&#x9879;&#x76EE;&#x4E2D;<code>urls.py</code>&#xFF1A;&#x53EA;&#x8981;&#x4E0D;&#x662F;<code>admin/</code>&#x5C31;&#x5339;&#x914D;&#x6210;&#x529F;&#xFF0C;&#x5305;&#x542B;&#x5230;&#x5E94;&#x7528;&#x4E2D;&#x7684;<code>urls.py</code></p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> path,include
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin

urlpatterns = [
     path(<span class="hljs-string">&apos;admin/&apos;</span>, include(admin.site.urls)),

     <span class="hljs-comment"># &#x53EA;&#x8981;&#x4E0D;&#x662F;&#x2018;admin/&#x2019;&#x5C31;&#x5339;&#x914D;&#x6210;&#x529F;&#xFF0C;&#x5305;&#x542B;&#x5230;&#x5E94;&#x7528;&#x4E2D;&#x7684;urls.py</span>
     path(<span class="hljs-string">&apos;&apos;</span>, include(<span class="hljs-string">&apos;book.urls&apos;</span>)),
 ]
</code></pre>
</li>
</ul>
</li>
<li><p>&#x5E94;&#x7528;&#x4E2D;<code>urls.py</code>&#xFF1A;&#x5339;&#x914D;<code>books/</code>&#x6210;&#x529F;&#x5C31;&#x8C03;&#x7528;<code>views</code>&#x4E2D;&#x7684;<code>BookListView&#x89C6;&#x56FE;</code>&#xFF0C;&#x6D4B;&#x8BD5;&#x9879;&#x76EE;&#x903B;&#x8F91;</p>
<pre><code class="lang-py"><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> re_path
<span class="hljs-keyword">from</span> book <span class="hljs-keyword">import</span> views
urlpatterns = [
    re_path(<span class="hljs-string">r&apos;^books/$&apos;</span>,views.BookListView.as_view()),
]
</code></pre>
</li>
<li><p>&#x6D4B;&#x8BD5;&#x9879;&#x76EE;&#x903B;&#x8F91;</p>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../01-day/restful/method/response.html" class="navigation navigation-prev " aria-label="Previous page: 响应相关"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../01-day/restful/django.html" class="navigation navigation-next " aria-label="Next page: 使用Django开发REST 接口"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
